<?php
namespace app\admin\controller;
use think\Controller;

Class AlternateImg extends BaseAdmin
{

	//首页轮播图列表
    public function lst()
    {
    	if(request()->isPost()){
            $data=input('post.');
            // dump($data['sort']);die;
            foreach ($data['sort'] as $k => $v) {
              db('tp_alternate_img')->where(['id'=>$k])->update(['sort'=>$v]);
            }
            $this->success('排序成功', 'AlternateImg/lst');
        }
        
        $AlternateImgRes=db('tp_alternate_img')->order('sort DESC')->paginate(5);
        $this->assign('AlternateImgRes',$AlternateImgRes);
        return $this->fetch('list');
    }

    //首页轮播图添加
    public function add()
    {

        if(request()->isPost()){
        	$data=input('post.');

        	if($data['link_url']&&stripos($data['link_url'],'http://')===false){
        		$data['link_url']='http://'.$data['link_url'];
        	}

        	if($_FILES['img_src']['tmp_name']){
        		$data['img_src']=$this->upload();
        	}


        	//验证
        	// $validate = new \app\admin\validate\CategoryAd;
        	// if (!$validate->check($data)) {
        	// 	$this->error($validate->getError());
        	// }
        	
        	$add=db('tp_alternate_img')->insert($data);
        	if($add){
        		$this->success('保存成功','lst');
        	}
        	else{
        		$this->error('保存失败');
        	}

        }
        return $this->fetch();

    }

    //首页轮播图编辑
    public function edit($id)
    {
    
    	$AlternateImgRes=db('tp_alternate_img')->where(array('id'=>$id))->find();
    	$this->assign([
            'AlternateImgRes'=>$AlternateImgRes,
          
        ]);
    	if(request()->isPost()){
        	$data=input('post.');
          
            // dump($_FILES);die;
        	if($data['link_url']&&stripos($data['link_url'],'http://')===false){
        		$data['link_url']='http://'.$data['link_url'];
        	}

        	if($_FILES['img_src']['tmp_name']){
                $oldBrandImg=IMG_UPLOADS.$AlternateImgRes['img_src'];
                if(file_exists($oldBrandImg)){
                    @unlink($oldBrandImg);
                }
        		$data['img_src']=$this->upload();
        	}
        	$up=db('tp_alternate_img')->where(array('id'=>$id))->update($data);
        	// dump($up);
        	// exit;
        	if($up!==false){
        		$this->success('保存成功','lst');
        	}
        	else{
        		$this->error('保存失败');
        	}

        }
        return $this->fetch();
    }

    //首页轮播图删除
    public function del($id)
    {
        $AlternateImgRes=db('tp_alternate_img')->where(array('id'=>$id))->find();
        $oldBrandImg=IMG_UPLOADS.$AlternateImgRes['img_src'];
        if(file_exists($oldBrandImg)){
            @unlink($oldBrandImg);
        }
        $del=db('tp_alternate_img')->delete($id);
        if($del){
          $this->success('删除成功','lst');

      }else{
        $this->error('删除失败');
    }
    
    return $this->fetch();
    }

    
    //图片上传
	public function upload(){
		$file=request()->file('img_src');
		if($file==null){
			exit(json_encode(array('msg'=>'没有文件上传')));
		}
		$info=$file->move('../public/static/uploads');
		$ext=$info->getExtension();//获取后缀名
		if(!in_array($ext,array('jpg','png','gif','jpeg'))){
			exit(json_encode(array('msg'=>'文件格式不符合')));
		}
		if($info){
			$imgSrc=$info->getSaveName();
            $imgSrc=str_replace('\\','/',$imgSrc);
            return $imgSrc;

		}else{
			$file->getError();
			die;
		}
		
		
	}


}